package trainning;

import java.util.*;

public class FightTheLandlord {
    public static void main(String[] args) {
        //1.组牌
        ArrayList<String> deck = getDeck();
        //打印牌组
        System.out.println("起始牌组为：");
        System.out.println(deck);
        System.out.println("---------------------------------------");

        //2.洗牌
        shuffleDeck(deck);
        System.out.println("---------------------------------------");
        
        //3.给牌编号
        HashMap<Integer, String> hm = getNumToDeck(deck);
        hm.forEach((key,value)-> System.out.println(key+" == "+value));
        System.out.println("---------------------------------------");
        
        //4.留底牌
        ArrayList<String> landlordCard = getLandlordCard(hm);
        System.out.println("底牌为：");
        System.out.println(landlordCard);
        System.out.println("---------------------------------------");
        //5.发牌
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();

        deal(hm, player1, player2, player3);

        //看牌
        System.out.println("第一位玩家手牌：");
        System.out.println(player1);
        System.out.println("第二位玩家手牌：");
        System.out.println(player2);
        System.out.println("第三位玩家手牌：");
        System.out.println(player3);
        System.out.println("---------------------------------------");

        //6.排序
        System.out.println("第一位玩家手牌整理后：");
        TreeSet<String> playerHand1 = cardSort(player1);

        System.out.println(playerHand1);
        System.out.println("第二位玩家手牌整理后：");
        TreeSet<String> playerHand2 = cardSort(player2);

        System.out.println(playerHand2);
        System.out.println("第三位玩家手牌整理后：");
        TreeSet<String> playerHand3 = cardSort(player3);
        System.out.println(playerHand3);
        System.out.println("---------------------------------------");
        //7.加入地主牌
        for (String card : landlordCard) {
            playerHand1.add(card);
        }
        System.out.println("地主手牌为：");
        System.out.println(playerHand1);




    }

    private static TreeSet<String> cardSort(ArrayList<String> player) {
        TreeSet<String> ts = new TreeSet<>((o1,o2)-> {
                int num1 = compareCard(o1);
                int num2 = compareCard(o2);
                int result = num1 - num2;
                result = result == 0?o1.substring(0,1).compareTo(o2.substring(0,1)):result;
                return result;
        });
        for (int i = 0; i < player.size(); i++) {
            ts.add(player.get(i));
        }
        return ts;
    }

    private static int compareCard(String s){
        int num;
        if(s.substring(1).equals("J")){
            num = 11;
        } else if (s.substring(1).equals("Q")) {
            num = 12;
        } else if (s.substring(1).equals("K")) {
            num = 13;
        } else if (s.substring(1).equals("A")){
            num = 14;
        } else if (s.substring(1).equals("2")) {
            num = 15;
        } else if (s.substring(1).equals("王")) {
            if(s.substring(0,1).equals("小")){
                num = 16;
            }else{
                num = 17;
            }

        } else {
            num = Integer.parseInt(s.substring(1));
        }
        return num;
    }


    private static void deal(HashMap<Integer, String> hm, ArrayList<String> player1, ArrayList<String> player2, ArrayList<String> player3) {
        hm.forEach((key, value)->{
            if(key < 51){
                if(key % 3 == 1){
                    player1.add(value);
                } else if (key % 3 == 2) {
                    player2.add(value);
                }else {
                    player3.add(value);
                }
            }
        });
    }

    private static ArrayList<String> getLandlordCard(HashMap<Integer, String> hm) {
        ArrayList<String> landlordCard = new ArrayList<>();
        landlordCard.add(hm.get(51));
        landlordCard.add(hm.get(52));
        landlordCard.add(hm.get(53));
        return landlordCard;
    }

    private static HashMap<Integer, String> getNumToDeck(ArrayList<String> deck) {
        HashMap<Integer,String> hm = new HashMap<>();
        int count = 0;
        for (String card : deck) {
            hm.put(count,card);
            count++;
        }
        return hm;
    }

    private static void shuffleDeck(ArrayList<String> deck) {
        for (int i = 1; i <=3; i++) {
            Collections.shuffle(deck);
            System.out.println("第"+i+"次洗牌：");
            System.out.println(deck);
        }
    }

    private static ArrayList<String> getDeck() {
        String [] hs = {"♥","♠","♦","♣"};
        String [] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        ArrayList<String> deck = new ArrayList<>();

        for (int i = 0; i < hs.length; i++) {
            for (int i1 = 0; i1 < num.length; i1++) {
                String card = hs[i]+num[i1];
                deck.add(card);
            }
        }
        deck.add("大王");//大王
        deck.add("小王");//小王
        return deck;
    }
}
